/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.pub.dao.OfficeItemsCollarDao;
import com.tech15.pub.dao.OfficeItemsDao;
import com.tech15.pub.model.OfficeItems;
import com.tech15.pub.model.OfficeItemsCollar;
import com.tech15.pub.vo.query.OfficeItemsCollarQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class OfficeItemsCollarManager extends BaseManager<OfficeItemsCollar,Long>{

	private OfficeItemsCollarDao officeItemsCollarDao;
	private OfficeItemsDao officeItemsDao;
	public void setOfficeItemsDao(OfficeItemsDao dao) {
		this.officeItemsDao = dao;
	}
	public void setOfficeItemsCollarDao(OfficeItemsCollarDao dao) {
		this.officeItemsCollarDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.officeItemsCollarDao;
	}
	
	public OfficeItemsCollar getById(Long id){
		OfficeItemsCollar oc = officeItemsCollarDao.getById(id);
		if(oc.getWpid() != null){
			OfficeItems oi = officeItemsDao.getById(oc.getWpid());
			oc.setWpmc(oi.getMc());
		}
		return oc;
	}
	@Transactional(readOnly=true)
	public Page findPage(OfficeItemsCollarQuery query) {
		return officeItemsCollarDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return officeItemsCollarDao.findByPageRequest(pr);
	}
	
	public void saveFf(OfficeItemsCollar officeItemsCollar) {
		
	}
	public boolean saveOC(OfficeItemsCollar officeItemsCollar) {
		if(ObjectUtils.isNotEmpty(officeItemsCollar.getWpid())){
			OfficeItems oi = officeItemsDao.getById(officeItemsCollar.getWpid());
			if(officeItemsCollar.getSl()>oi.getSl()){
				return false;
			}else{
				oi.setSl(oi.getSl() - officeItemsCollar.getSl());
				officeItemsDao.update(oi);
			}
		}
		officeItemsCollarDao.save(officeItemsCollar);
		return true;
	}
	
}
